<?php

/* Note: This thumbnail creation script requires the GD PHP Extension.  
  If GD is not installed correctly PHP does not render this page correctly
  and SWFUpload will get "stuck" never calling uploadSuccess or uploadError
 */

// Get the session Id passed from SWFUpload. We have to do this to work-around the Flash Player Cookie Bug
if (isset($_POST["PHPSESSID"])) {
    session_id($_POST["PHPSESSID"]);
}

//sleep(3);
ini_set("html_errors", "0");

// Check the upload
if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
    echo "ERROR:invalid upload";
    exit(0);
}

//Defined folder
$thumbFolder = '/photos/thumbnail/';
$largeFolder = '/photos/large/';

$uploadUrl = strftime('%Y/%m/%d/');

//Make thumb dir
$thumb_url = $thumbFolder . $uploadUrl;
$thumb_dir = $_SERVER['DOCUMENT_ROOT'] . $thumb_url;
if (!is_dir($thumb_dir)) {
    mkdir(str_replace('//', '/', $thumb_dir), 0777, true);
}

//Make large dir
$large_url = $largeFolder . $uploadUrl;
$large_dir = $_SERVER['DOCUMENT_ROOT'] . $large_url;
if (!is_dir($large_dir)) {
    mkdir(str_replace('//', '/', $large_dir), 0777, true);
}

//Get file name & extension
$file_id = md5($_FILES["Filedata"]["tmp_name"] + rand() * 100000);
$file_ext = strtolower(substr($_FILES["Filedata"]["name"], -3));

//Check extension
if (!in_array($file_ext, array('jpg', 'gif', 'bmp'))) {
    echo "ERROR:invalid extension";
    exit(0);
}

//Set file name
$file_name = $file_id . '.' . $file_ext;

//Set option
$options = array(
    'thumb_width' => 290,
    'thumb_height' => 390
);

// Get the image and create a thumbnail
$src_img = imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);
if (!$src_img) {
    echo "ERROR:could not create image handle " . $_FILES["Filedata"]["tmp_name"];
    exit(0);
}

$img_width = imageSX($src_img);
$img_height = imageSY($src_img);

if (!$img_width || !$img_height) {
    echo "ERROR:Invalid width or height";
    exit(0);
}

$scale = min(
        $options['thumb_width'] / $img_width, $options['thumb_height'] / $img_height
);

$new_width = $img_width * $scale;
$new_height = $img_height * $scale;
$new_img = @imagecreatetruecolor($new_width, $new_height);
$write_image = 'imagejpeg';
$image_quality = 100;

$thumb_result = $src_img && @imagecopyresampled(
                $new_img, $src_img, 0, 0, $x_offset, $y_offset, $new_width, $new_height, $img_width, $img_height
        ) && $write_image($new_img, $thumb_dir . $file_name, $image_quality);
// Free up memory (imagedestroy does not delete files):
@imagedestroy($src_img);
@imagedestroy($new_img);

//Upload to large photos
$large_result = move_uploaded_file($_FILES["Filedata"]["tmp_name"], $large_dir . $file_name);

if (!$thumb_result || !$large_result) {
    echo "ERROR:upload error";
    exit(0);
}

echo json_encode(array(
    'error' => 0,
    'message' => 'Success',
    'data' => array(
        'name' => substr($_FILES["Filedata"]["name"], 0, -4),
        'large' => $large_url . $file_name,
        'thumb' => $thumb_url . $file_name
    )
));
exit();
?>